﻿using Curriculum.DataSet;
using DevExpress.XtraCharts;
using DevExpress.XtraEditors;

namespace Curriculum.Controls
{
    public partial class OverviewXtraUserControl : XtraUserControl
    {
        private BackOfficeController _controller;

        #region Properties

        public BackOfficeController Controller
        {
            set
            {
                _controller = value;
                SetGridDataSource();                
            }
        }


        #endregion

        public OverviewXtraUserControl()
        {
            InitializeComponent();
        }

        public void Reload()
        {
            SetGridDataSource();
        }
        
        #region Private Methods

        private void SetGridDataSource()
        {
            AddSeries(companiesChartControl, 0, _controller.GetSexCount());
            AddSeries(companiesChartControl, 1, _controller.GetEducationCount());
            AddSeries(companiesChartControl, 2, _controller.GetAgeCount());
            AddSeries(citiesChartControl, 0, _controller.GetCitiesCount());
            AddSeries(positionsChartControl, 0, _controller.GetPositionsCount());
        }

        private static void AddSeries(ChartControl chartControl, int series, OverviewDataSet data)
        {
            chartControl.Series[series].Points.Clear();
            foreach (var overview in data.Overview)
            {
                var argument = overview.IsNameNull() || overview.Name == "" ? " " : overview.Name;
                var point = new SeriesPoint(argument) {Values = new double[1]};
                point.Values[0] = overview.Total;
                chartControl.Series[series].Points.Add(point);
            }
        }

        #endregion
    }
}